//
//  HistoryVC.m
//  Lrjh
//
//  Created by jun on 2025/7/21.
//

#import "HistoryVC.h"
#import "HistoryCell.h"
#import "UserModel.h"
#import "MapViewVC.h"

@interface HistoryVC ()<BaseVCDelegate>

@property(nonatomic,strong) NSMutableArray *datas;
@end

@implementation HistoryVC

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = RGBHex(0xefefef);
    [self.view addSubview:self.collectionView];
    [self.collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.top.bottom.mas_offset(0);
        
    }];
    self.datas = [NSMutableArray array];
    self.delegate = self;
    [self registerCell:@"HistoryCell"];
    [self history];
}

-(NSInteger)numberOfSections:(NSInteger)section{
    return self.datas.count;
}

-(CGSize)itemSize:(NSIndexPath *)indexPath{
    CGFloat width = SCRENNWIDTH - 30;
    return CGSizeMake(width, 150.f);
}

-(CGFloat)lineSpacing:(NSInteger)section{
    return 15.f;
}
-(UIEdgeInsets)sectionMargin:(NSInteger)section{
    return UIEdgeInsetsMake(15, 15, 15, 15);
}

-(UICollectionViewCell *)cell:(UICollectionView *)collectionView indexPath:(NSIndexPath *)indexPath{
    HistoryCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"HistoryCell" forIndexPath:indexPath];
    UserModel *model = self.datas[indexPath.row];
    cell.labDesc.text = model.des;
//    cell.labAddress.text = model.location;
    cell.labTime.text= model.punchTime;
    cell.labLatLon.text = [NSString stringWithFormat:@"%@,%@,%@",model.longitude,model.latitude,model.location];
    [cell.ivImage sd_setImageWithURL:[NSURL URLWithString:model.punchImg]];
    return cell;
}


-(void)onItemClick:(NSIndexPath *)indexPath{
    UserModel *model = self.datas[indexPath.row];
    MapViewVC *vc = [MapViewVC new];
    vc.lat = [model.latitude floatValue];
    vc.lon = [model.longitude floatValue];
    [self go2VC:vc];
}

-(void)history{
    NSDictionary *parm = @{
        @"page":@"1",
        @"size":@"30"
    };
    
    NSString *url = @"https://www.hyjzzx.cn/api/app/punch/list";
    MB_LOADING(@"Progress...");
    [LEHttp httpPost:url token:self.token param:parm response:^(NSInteger code, NSString *message, id data) {
        MB_HIDE();
        if(code==1){
            NSDictionary *dataObj = data[@"data"];
            
            [self.datas removeAllObjects];
            NSArray *models = [NSArray yy_modelArrayWithClass:[UserModel class] json:dataObj[@"list"]];
            if(models!=nil){
                [self.datas addObjectsFromArray:models];
            }
            [self.collectionView reloadData];
            
        }else{
            [self showTipInfo:message okClick:^{
                [self.navigationController popViewControllerAnimated:YES];
            }];
        }
    }];
}

@end
